import 'package:flutter/material.dart';

class Widget_SnackBar_Page extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Widget_SnackBar_State();
  }
}

class Widget_SnackBar_State extends State<Widget_SnackBar_Page> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("SnackBar"),
        ),
        floatingActionButton: Builder( //当BuildContext在Scaffold之前时，调用Scaffold.of(context)会报错。这时可以通过Builder Widget来解决
            builder: (BuildContext context) {
              return FloatingActionButton(
                onPressed: () {
                  Scaffold.of(context).showSnackBar(
                      SnackBar(
                        content: Text("这是一个SnackBar"),
                        backgroundColor: Colors.red, //设置背景颜色
                        duration: Duration(
                            milliseconds: 1
                        ),
                        action: SnackBarAction(
                            label: "别点我",
                            onPressed: () {
                              print("SnackBar上面的按钮被点击了");
                            }
                        ),
                      )
                  );
                },
                child: Text("click"),
              );
            }),
      ),
    );
  }
}


/**
    const SnackBar({
    Key key,
    @required this.content,//显示的内容
    this.backgroundColor,//设置背景颜色
    this.action,//可以添加事件
    this.duration = _kSnackBarDisplayDuration,
    this.animation,
    })
 */